# -*- coding: utf-8 -*-

{% for model_import in model_import_list %}
{{ model_import }}
{% endfor %}
{% if table.sub %}
from sqlalchemy.orm import relationship
{% endif %}
from typing import Optional
from sqlalchemy.orm import Mapped, mapped_column

from app.core.base_model import CreatorMixin


class {{ class_name }}Model(CreatorMixin):
    """
    {{ function_name }}表
    """
    __tablename__ = '{{ table_name }}'
    __table_args__ = {'comment': '{{ function_name }}'}
    __loader_options__ = ["creator"]

    {% for column in columns %}
    {% if column.column_name not in ['id', 'creator_id', 'description', 'created_at', 'updated_at'] %}
    {{ column.column_name }}: Mapped[Optional[{{ column.python_type }}]] = mapped_column({{ column.column_type|get_sqlalchemy_type }}, {% if column.pk %}primary_key=True, {% endif %}{% if column.increment %}autoincrement=True, {% endif %}{% if column.required or column.pk %}nullable=False{% else %}nullable=True{% endif %}, comment='{{ column.column_comment }}')
    {% endif %}
    {% endfor %}

    {% if table.sub %}
    {{ sub_class_name }}_list = relationship('{{ sub_class_name }}', back_populates='{{ business_name }}')
    {% endif %}
